dojo.provide("slipstream.view.Entry");
dojo.require("dijit.layout._TemplatedLayoutWidget");
dojo.require("dijit._Container");
dojo.require("dijit.Editor");
dojo.require("dijit.layout.BorderContainer");
dojo.require("dijit.layout.ContentPane");
dojo.require("dojox.grid.DataGrid");
dojo.require("dijit.layout.AccordionContainer");
dojo.require("dojox.form.MultiComboBox");
dojo.require("dojox.layout.ExpandoPane");
dojo.require("dojo.date.locale");
dojo.require("dijit._editor.plugins.FontChoice");
dojo.require("dijit._editor.plugins.LinkDialog");

dojo.declare("slipstream.view.Entry",
	[dijit.layout._TemplatedLayoutWidget], {
		
	templatePath: dojo.moduleUrl("slipstream.view", "templates/EntryClean.html"),
	
	forms: [],
	store: null,
	autosave: false,
	structure: null,
	activeItem: null,
	formConnections: [],
	widgetsInTemplate: true,
	doLayout: true,
	baseClass: "dijitContentPane",
	
	postCreate: function(){
		this.inherited(arguments);
		this.isLoaded=true;
		if(this.grid) dojo.connect(this.grid, 'onRowClick', this, '_onGridClick');
		var self=this;
		if(!this.autoSave && this.buttonBar){
			this.saveButton = new dijit.form.Button({
				label: "Spara",
				onClick: dojo.hitch(this,function(e){
					this._forceValuesToStore();
					if(this.store.isDirty()){this.store.save()}
				})
			});
			this.revertButton = new dijit.form.Button({
				label: "Återställ",
				onClick: dojo.hitch(this,function(e){
					this.store.revert();
					this._setItemAttr(this.item);
				})
			});
			this.buttonBar.domNode.appendChild(this.saveButton.domNode);
			this.buttonBar.domNode.appendChild(this.revertButton.domNode);
			if(this.slugsource) this.slugsource.intermediateChanges=true;
		};
		//this._layoutChildren();
	},/*
	_onShow: function(){
		console.debug('_onShow',this.selected);
		this.startup();
	},
	startup: function(){
		console.debug('startup',this.selected);
		if(this.selected) this.inherited(arguments);
	},*/
	_onSetItem: function(item){
		this.activeItem=item;
	},
				layout: function(){
					if(this.getChildren().length==1){
						console.debug('ContentBox:',this._contentBox);
						this.getChildren()[0].resize(this._contentBox);
						//this.getChildren()[0].resize();
					} else {
						console.error('Can only contain one child, have '+this.getChildren().length,this);
					}
				},
	
	/*layout: function(){
		this._scheduleLayout();
	},*/
	_onGridClick: function(e){
		console.debug('Dirty before',this.store.isDirty());
		this._storeSaveRollback();
		this.store.loadItem({
			item: e.grid.getItem(e.rowIndex),
			onItem: dojo.hitch(this, "_setItemAttr"),
		});
		this.browse.toggle();
		if(!this.detail._showing) this.detail.toggle();
	},
	
	_setItemAttr: function(item){
		this.item=item;
		dojo.forEach(this.formConnections,function(cn){dojo.disconnect(cn)});
		var store=this.store;
		var formConnections=this.formConnections;
		dojo.forEach(this.forms,function(node,pos){
			node.attr('value',store.getValue(item,node.name));
			formConnections.push(dojo.connect(node,"onChange",function(){
				store.setValue(item,node.name,node.attr('value'));
			}))
		});
		if (this.slugsource && this.slug) {
			if ((this.slug.attr('value') == '') || (this.slug.attr('value') == convertToSlug(this.slugsource.attr('value')))) {
				this.slugConnection = dojo.connect(this.slugsource, 'onChange', this, function(e){
					this.slug.attr('value', convertToSlug(this.slugsource.attr('value')));
				});
			}
			formConnections.push(dojo.connect(this.slug, "onChange", this, function(){
				if (this.slug.attr('value') == '') {
					this.slug.attr('value', convertToSlug(this.slugsource.attr('value')));
					if (!this.slugConnection || !this.slugConnection[0]) {
						this.slugConnection = dojo.connect(this.slugsource, 'onChange', this, function(e){
							this.slug.attr('value', convertToSlug(this.slugsource.attr('value')));
						});
					}
				}
				else 
					if (this.slug.attr('value') != convertToSlug(this.slugsource.attr('value'))) {
						dojo.disconnect(this.slugConnection);
					}
			}))
		}
	},
	
	_onChangeSlug: function(){
		if(this.slug.attr('value')=='') this.slug.attr('value',convertToSlug(this.slugsource.attr('value')));
	},
	
	_forceValuesToStore: function(){
		if (this.item) {
			dojo.forEach(this.forms, function(node){
				if(node.attr('value') instanceof Date){
					if (this.store.getValue(this.item, node.name).getTime() != node.attr('value').getTime()) {
						console.debug('Changed node '+node.name);
						this.store.setValue(this.item, node.name, node.attr('value'));
					}
				} else {
					if (this.store.getValue(this.item, node.name) != node.attr('value')) {
						console.debug('Changed node ',node.name,'to',node.attr('value'),'from',this.store.getValue(this.item, node.name));
						this.store.setValue(this.item, node.name, node.attr('value'));
					};
				};
			}, this);
		}
	},
	
	_storeSaveRollback: function(){
		console.debug('Dirty before',this.store.isDirty());
		this._forceValuesToStore();
		console.debug('Dirty after',this.store.isDirty());
		if(this.store.isDirty()){
			if(confirm('Vill du spara dina ändringar?')){
				this.store.save();
			} else {
				this.store.revert();
			}
		}
	},
/*
	_onShow: function(){
		if(this._needLayout){
			this._layoutChildren();
		}

		if(this.onShow){
			this.onShow();
		}
	},

	startup: function(){
		if(this._started){ return; }

		dojo.forEach(this.getChildren(), function(child){
			child.startup();
		});
		if(this.doLayout){
			this._checkIfSingleChild();
		}
		if(!this._singleChild || !dijit._Contained.prototype.getParent.call(this)){
			this._scheduleLayout();
			}
		this.inherited(arguments);
	},
	
	resize: function(){
		return dijit.layout.ContentPane.prototype.resize.apply(this, arguments);
	},
	
	_layoutChildren: function(){
		return dijit.layout.ContentPane.prototype._layoutChildren.apply(this, arguments);
	},
	
	_scheduleLayout: function(){
		return dijit.layout.ContentPane.prototype._scheduleLayout.apply(this, arguments);
	},
	
	_isShown: function(){
		return dijit.layout.ContentPane.prototype._isShown.apply(this, arguments);
	},
	
	_checkIfSingleChild: function(){
		return dijit.layout.ContentPane.prototype._checkIfSingleChild.apply(this, arguments);
	}*/
});
